//
//  ReceivingAddressController.m
//  ios-supplier
//
//  Created by Benlai on 15/8/4.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "ReceivingAddressController.h"
#import <AMapSearchKit/AMapSearchAPI.h>
#import "ReceivingAddressCell.h"
#import "JFBStoreInfoDS.h"

#warning 接手地址
@interface ReceivingAddressController () <AMapSearchDelegate, UISearchBarDelegate,UIGestureRecognizerDelegate>

@property (nonatomic, strong)UITableView *searchTableView;
@property (nonatomic, strong)NSArray *filterArray;
@property (nonatomic, strong)NSMutableArray *addressArray;
@property (nonatomic, strong)AMapSearchAPI *searchApi;
@property (nonatomic, strong)AMapPlaceSearchRequest *searchRequest;
@end

@implementation ReceivingAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.filterArray = [[NSArray alloc] init];
    self.addressArray = [[NSMutableArray alloc] init];
    
    [self createSearchTableView];
    
    [self creachSearchBar];
    
    [self createSearchApiAndRequest];
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationItem setLeftBarButtonItem: [self createBackView]];
}


- (UIBarButtonItem *)createBackView
{
    UIButton *leftButtonItem = [UIButton buttonWithType:UIButtonTypeCustom];
    
    leftButtonItem.frame = CGRectMake(0, 0, 11, 20);
    
    [leftButtonItem setBackgroundImage:[UIImage imageNamed:@"icon-Back"] forState:UIControlStateNormal];
    
    [leftButtonItem setImageEdgeInsets: UIEdgeInsetsMake((19 - 11) / 2, 0, (19 - 11) / 2, 0)];
    
    [leftButtonItem addTarget:self action:@selector(leftButtonItemClicked) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:leftButtonItem];
    
    return item;
}

-(void)leftButtonItemClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)createSearchTableView
{
    self.searchTableView = [[UITableView alloc]initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.searchTableView.delegate = self;
    self.searchTableView.dataSource = self;
    self.searchTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.searchTableView registerNib: [UINib nibWithNibName: @"ReceivingAddressCell" bundle: nil] forCellReuseIdentifier: @"ReceivingAddressCell"];

    [self.view addSubview: self.searchTableView];
}

-(void)creachSearchBar
{
    UIView *searchBg = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 50)];
    UILabel *line = [[UILabel alloc] initWithFrame: CGRectMake(0, searchBg.frame.size.height - 1, searchBg.frame.size.width, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [searchBg addSubview: line];
    
    
    UITextField *searchTextField = [[UITextField alloc]initWithFrame: CGRectMake(10, 5, searchBg.frame.size.width - 20, searchBg.frame.size.height - 10)];
    
    [searchTextField setPlaceholder: @"请选择您的住宅小区、大厦或学校"];
    [searchTextField setFont:[UIFont systemFontOfSize:14]];
    [searchTextField setLeftViewMode: UITextFieldViewModeAlways];
    [searchTextField setBackgroundColor: [UIColor colorFromHexValue:0xf1f2f6]];
    [searchTextField setTextColor:[UIColor colorFromHexValue:0x666666]];
    searchTextField.layer.cornerRadius = 5;
    [searchBg addSubview: searchTextField];

    UIView *searchBtnContainer = [[UIView alloc] initWithFrame: CGRectMake(0, 0, searchTextField.frame.size.height, searchTextField.frame.size.height)];    
    [searchTextField setLeftView: searchBtnContainer];
    
    UIButton *btnSearch=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 14, 14)];
    [btnSearch setImage:[UIImage imageNamed:@"icon-Search.png"] forState:UIControlStateNormal];
    [btnSearch setContentMode:UIViewContentModeScaleAspectFit];
    [btnSearch setCenter: searchBtnContainer.center];
    [searchBtnContainer addSubview: btnSearch];
    
    [searchTextField addTarget: self action: @selector(textDidChange:) forControlEvents: UIControlEventEditingChanged];
    
    self.searchTableView.tableHeaderView = searchBg ;
}

-(void)createSearchApiAndRequest
{
    self.searchApi = [[AMapSearchAPI alloc] initWithSearchKey: ammapApiKey Delegate: self];
    
    self.searchRequest = [[AMapPlaceSearchRequest alloc] init];
    self.searchRequest.searchType = AMapSearchType_PlaceKeyword;
    JFBStoreInfoDS *infoDS = [JFBStoreInfoDS loadFromDB];
    self.searchRequest.city = @[infoDS.city_name];
    self.searchRequest.requireExtension = NO;
    self.searchRequest.offset = 15;
}

-(void) textDidChange:(id) sender
{
    UITextField *txt=(UITextField *)sender;
    if(txt.text.length > 0)
    {
        self.searchRequest.keywords = txt.text;
        [self.searchApi AMapPlaceSearch: self.searchRequest];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    self.searchRequest.keywords = searchText;
    [self.searchApi AMapPlaceSearch: self.searchRequest];
}


-(void)onPlaceSearchDone:(AMapPlaceSearchRequest *)request response:(AMapPlaceSearchResponse *)response
{
    if (response.pois.count == 0) {
        return;
    }
    
    self.filterArray = response.pois;
    [self.searchTableView reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return self.filterArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"ReceivingAddressCell";
    ReceivingAddressCell *cell = (ReceivingAddressCell *)[tableView dequeueReusableCellWithIdentifier:cellId];
    
    
    if (self.filterArray.count > 0) {
         AMapPOI *poi=[self.filterArray objectAtIndex:indexPath.row];
      
        cell.nameLabel.text = poi.name;
        cell.addressLabel.text = poi.address;
    
    }

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AMapPOI *poi=[self.filterArray objectAtIndex:indexPath.row];
    [self.delegate selectedRegion: poi.name];
    [self.navigationController popViewControllerAnimated: YES];
}


@end
